Java中List,Integer[],int[]的相互转换

学习过程中总是遇见List,Integer[],int[] 等之间的转换问题让人头疼,下面提供一些方法,希望能帮到您,如果你有更好的方法,欢迎评论区留言。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
int[] data = {4, 5, 3, 6, 2, 5, 1};

//1. int[] 转 List<Integer>
List<Integer> list1 = Arrays.stream(data).boxed().collect(Collectors.toList());

// Arrays.stream(arr) 可以替换成IntStream.of(arr)。
// 1.使用Arrays.stream将int[]转换成IntStream。
// 2.使用IntStream中的boxed()装箱。将IntStream转换成Stream<Integer>。
// 3.使用Stream的collect(),将Stream<T>转换成List<T>,因此正是List<Integer>。

//2. int[] 转 Integer[]
Integer[] integers1 = Arrays.stream(data).boxed().toArray(Integer[]::new);
// 前两步同上,此时是Stream<Integer>。
// 然后使用Stream的toArray,传入IntFunction<A[]> generator。
// 这样就可以返回Integer数组。
// 不然默认是Object[]。

//3. List<Integer> 转 Integer[]
Integer[] integers2 = list1.toArray(new Integer[0]);

// 调用toArray。传入参数T[] a。这种用法是目前推荐的。
// List<String>转String[]也同理。

//4. List<Integer> 转 int[]
int[] arr1 = list1.stream().mapToInt(Integer::valueOf).toArray();
// 想要转换成int[]类型,就得先转成IntStream。
// 这里就通过mapToInt()把Stream<Integer>调用Integer::valueOf来转成IntStream
// 而IntStream中默认toArray()转成int[]。

//5. Integer[] 转 int[]
int[] arr2 = Arrays.stream(integers1).mapToInt(Integer::valueOf).toArray();
// 思路同上。先将Integer[]转成Stream<Integer>,再转成IntStream。

//6. Integer[] 转 List<Integer>
List<Integer> list2 = Arrays.asList(integers1);

// 最简单的方式。String[]转List<String>也同理。
// 同理 String[] strings1 = {"a", "b", "c"};

//7. String[] 转 List<String>
List<String> list3 = Arrays.asList(strings1);

//8. List<String> 转 String[]
String[] strings2 = list3.toArray(new String[0]);
}
}
hey!baby,站住,点它!